import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterxc/module/common_module.dart';
import 'package:flutterxc/module/gridnav_module.dart';
import 'package:flutterxc/widget/webview.dart';
class GridNav extends StatelessWidget {
  final GridNavModel gridNavModel;
  final String name;
  GridNav({@required this.gridNavModel,this.name = 'xiaoming'});
  @override
  Widget build(BuildContext context) {
//    print('${gridNavModel.hotel.startColor} 123');
    // TODO: implement build
    return PhysicalModel(
      color:Colors.transparent ,
      borderRadius: BorderRadius.circular(6),
      clipBehavior: Clip.antiAlias,
      child:Column(
        children: gridNavItems(context),
      )
    );
  }
//  返回三个 上中下 item
  List<Widget> gridNavItems(context){
    List<Widget> item = [];
  if (gridNavModel == null) return item;
  if (gridNavModel.hotel != null) {
    item.add(_gridNavItem(context,gridNavModel.hotel,true));
  }
    if (gridNavModel.flight != null) {
      item.add(_gridNavItem(context,gridNavModel.flight,false));
    }
    if (gridNavModel.travel != null) {
      item.add(_gridNavItem(context,gridNavModel.travel,false));
    }
    return item;
  }
  _gridNavItem(BuildContext contenx,GridNavItem model,bool first) {
    List<Widget> items = [];
    items.add(mainitem(contenx,model.mainItem));
    items.add(doubleitem(contenx, model.item1,model.item2));
    items.add(doubleitem(contenx, model.item3,model.item4));
    List<Widget> expandItem=[];

    items.forEach((item){
      expandItem.add(Expanded(child: item,flex: 1,));
    });

    Color startColor = Color(int.parse('0xff'+model.startColor));
    Color endColor = Color(int.parse('0xff'+model.endColor ));
    return Container(
      height: 88,
      margin:first?null: EdgeInsets.only(top: 3),
      decoration: BoxDecoration(
          gradient: LinearGradient(colors: [startColor,endColor])
      ),
      child: Row(children: expandItem,),
    );
  }
//  包含文字和图片的 单元格
  mainitem(BuildContext contenx,CommonModel model){
    return _wrapGesture(contenx, Stack(
      alignment: AlignmentDirectional.topCenter,
      children: <Widget>[
        Image.network(model.icon,width: 122,height:88,fit: BoxFit.contain, alignment: AlignmentDirectional.bottomEnd,),
        Container(
          margin: EdgeInsets.only(top: 12),
          child:Text(model.title,style: TextStyle(color: Colors.white),),
        )
      ],
    ), model);
  }
//  包含两个 文字格子的 单元格
  doubleitem(BuildContext contenx,CommonModel Topmodel,CommonModel Bottommodel){
    return Column(
      children: <Widget>[
        Expanded(
          child: _item(contenx,Topmodel,true),
        ),
        Expanded(
          child: _item(contenx,Bottommodel,false),
        )
      ],
    );
  }
//  每个只包含文字的格子
  _item(BuildContext contenx,CommonModel item,bool first) {
    BorderSide border_side = BorderSide(width: 0.8,color: Colors.white);
    return FractionallySizedBox(
      widthFactor: 1,
      child:Container(
        decoration: BoxDecoration(
          border:Border(
            left:border_side,
            bottom: first ? border_side:BorderSide.none
          )
        ),
        child: _wrapGesture(contenx, Center(child: Text(item.title,textAlign: TextAlign.center,style: TextStyle(color: Colors.white),),),item)
      ) ,
    );
  }
//  每个格子点击事件处理 跳转到浏览器
  _wrapGesture(contenx,Widget widget,CommonModel item) {
    return GestureDetector(
      onTap: (){
        Navigator.push(contenx, MaterialPageRoute(builder: (contenx)=>WebView(url: item.url,statusBarColor: item.statusBarColor,title: item.title,hideAppBar: item.hideAppBar,)));
      },
      child: widget,
    );
  }
}